<?php

namespace Tiger\Api\Commands;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputArgument;

class MakeApiAuthControllerCommand extends GeneratorCommand
{
    //利用GeneratorCommand的接口实现make功能
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'api:make-auth-controller';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Make api auth  controller ';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {

        //$this->type = $this->parseName($this->getNameInput());

        parent::fire();

        $this->call('api:make-auth-route', ['name' => $this->getNameInput()]);

        $this->call('api:make-model', ['name' => $this->getNameInput()]);

        $this->call('api:make-transformer', ['name' => $this->getNameInput()]);
        //拷贝root文件
    }


    /**
     * Get the desired class name from the input.
     *
     * @return string
     */
    protected function getNameInput()
    {
        return ucfirst(trim($this->argument('name')));
    }

    /**
     * Get the destination class path.
     *
     * @param  string  $name
     * @return string
     */
    protected function getPath($name)
    {
        $name = str_replace_first($this->laravel->getNamespace(), '', $name);

        return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'Controller.php';
    }

    /**
     * Replace the class name for the given stub.
     *
     * @param string $stub
     * @param string $name
     *
     * @return string
     */
    protected function replaceClass($stub, $name)
    {
        $stub = parent::replaceClass($stub, $name);

        $stub= str_replace(
            'dummy',
            lcfirst($this->getNameInput()),
            $stub
        );

        return str_replace(
            'Dummy',
            $this->getNameInput(),
            $stub
        );
    }

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return __DIR__.'/stubs/AuthController.stub';
    }

    /**
     * Get the default namespace for the class.
     *
     * @param string $rootNamespace
     *
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        $directory = config('tiger-api.api_directory');

        $namespace = ucfirst(basename($directory));
        $name=$this->getNameInput();

        return $rootNamespace."\\$namespace\\V1\\Controllers\\Auth$name";
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [
            ['name', InputArgument::REQUIRED, 'The name of the controller.'],
        ];
    }

}
